<?php
/**
 * 教育培训小程序模块微站定义
 *
 * @author 武汉志汇科技
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
require IA_ROOT.'/addons/zh_jypx/inc/func/core.php';
class Zh_jypxModuleSite extends Core {

	public function doMobileSchool(){
		global $_W,$_GPC;
		$school = pdo_getall('snx_school',array('uniacid'=>$_W['uniacid'],'state'=>2),array('id','cate_name'));
		if (empty($school)) {
			echo json_encode(array('msg'=>'暂无学校','code'=>'500'),320);exit();
		}
		// var_dump($school);die;
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$school),320);exit();
	}

	public function doMobileCourseCate(){
		global $_W,$_GPC;
		$coursecate = pdo_getall('snx_coursecate',array('uniacid'=>$_W['uniacid'],'school_id'=>$_GPC['school_id']),array('id','school_id','course_name'));
		if (empty($coursecate)) {
			echo json_encode(array('msg'=>'暂无学级','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$coursecate),320);exit();
	}

	public function doMobileCourse(){
		global $_W,$_GPC;
		$course = pdo_getall('snx_course',array('uniacid'=>$_W['uniacid'],'c_id'=>$_GPC['c_id']),array('id','c_id','name'));
		if (empty($course)) {
			echo json_encode(array('msg'=>'暂无课程','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$course),320);exit();
	}

	public function doMobileClass(){
		global $_W,$_GPC;
		$class = pdo_getall('snx_class',array('uniacid'=>$_W['uniacid'],'cou_id'=>$_GPC['cou_id']),array('id','class_name'));
		if (empty($class)) {
			echo json_encode(array('msg'=>'暂无班级','code'=>'500'),320);exit();
		}
		echo json_encode($class,320);die;
	}

	// public function doMobileTeacher(){
	// 	global $_W,$_GPC;
	// 	$data['username']=$_GPC['username'];
	// 	$data['teacher_name']=$_GPC['teacher_name'];
	// 	$data['password']=$_GPC['password'];
	// 	$data['class_id']=$_GPC['class_id'];
	// 	$data['state']=$_GPC['state'];
	// 	$data['create_time']=date('Y-m-d H:i:s',time());
	// 	$data['uniacid']=$_W['uniacid'];
	// 	if($_GPC['id']==''){
	// 		 $rst=pdo_insert('snx_teacher',$data);
	// 		}else{
	// 		$rst=pdo_update('snx_teacher',$data,array('id'=>$_GPC['id']));
	// 		}
	// 	if($rst){
	// 		echo json_encode(array('msg'=>'获取成功','code'=>'200'),320);exit();
	// 	}else{
	// 		echo json_encode(array('msg'=>'暂无班级','code'=>'500'),320);exit();
	// 	}
	// }
	
	 //批量通过
    public function doMobileAdoptUser(){
        global $_W, $_GPC;
        function getaccess_token($_W){
          $res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msgs($_W,$res_id){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
            $openid=pdo_get('snx_users',array('id'=>$res_id));
            $formwork ='{
                "touser": "'.$openid["openid"].'",
                "template_id": "'.$ress["templeidreg"].'",
                "form_id":"'.$openid['rformid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$openid['phone_number'].'",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"'.$openid['students_name'].'",
                        "color": "#173177"
                    },
                    "keyword3": {
                        "value":"注册成功，欢迎加入SUNNY英语大家庭!",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        for ($i=0; $i <count($_GPC['id']) ; $i++) {
            set_msgs($_W,$_GPC['id'][$i]);
        	pdo_update('snx_users', array('state' => 2), array('id' => $_GPC['id'][$i]));
        
        }
    }


    //批量删除
    public function doMobileDeleteTeacher(){
        global $_W, $_GPC;
        $res=pdo_delete('snx_teacher',array('id'=>$_GPC['id']));
        // pdo_delete('wxc_task',array('order_id'=>$_GPC['id']));
        if($res){
            message('删除成功',$this->createWebUrl('order',array()),'success');
        }else{
            message('删除失败','','error');
        }
    }

    //批量删除
    public function doMobileDeleteTask(){
        global $_W, $_GPC;
        $res=pdo_delete('snx_task',array('id'=>$_GPC['id']));
        if($res){
            message('删除成功',$this->createWebUrl('task',array()),'success');
        }else{
            message('删除失败','','error');
        }
    }

    /**
    *搜索核销员
    **/
    public function doMobileSelectUser(){
    global $_W, $_GPC;
    $sql =" select id,teacher_name from ".tablename('snx_teacher')." where uniacid={$_W['uniacid']} and id  not in (select hx_id  from" .tablename('snx_hx')."where uniacid={$_W['uniacid']} )  and  teacher_name like '%{$_GPC['keywords']}%'";    
    echo json_encode(pdo_fetchall($sql));
  }
}